package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.Setmeal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    DishMapper dishMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        List<ShoppingCart> list= shoppingCartMapper.queryByUserIdAndId(shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //若存在，将该商品数量加一
        if(Objects.nonNull(list) && !list.isEmpty()){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else {
            //若不存在，查询表单补全entity，存储至表单中
            //判断该购物车存储的是菜品还是套餐
            if(Objects.nonNull(shoppingCart.getSetmealId())){
                Setmeal setmeal = setmealMapper.queryById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }else{
                Dish dish = dishMapper.queryById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> queryAll() {
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartMapper.queryAll(currentId);
    }

    @Override
    public void deleteItem(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //如果数量等于1，则删除数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryByUserIdAndId(shoppingCart);
        ShoppingCart cartTemp = shoppingCarts.get(0);
        if(cartTemp.getNumber() == 1){
            shoppingCartMapper.deleteItem(cartTemp);
        }else{
            cartTemp.setNumber(cartTemp.getNumber() - 1);
            shoppingCartMapper.update(cartTemp);
        }
        //如果数量大于1，则数量-1

    }

    @Override
    public void deleteAll() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);
        shoppingCartMapper.deleteItem(shoppingCart);
    }
}
